package com.myapp.ruyixing.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.DirectMessageList;
import com.myapp.ruyixing.model.DirectMessageList.ADirectMessage;
import com.myapp.ruyixing.utils.BitmapLruCache;

public class DirectMessageFragment extends ListFragment {

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			DirectMessageList data = (DirectMessageList) o;

			if (data != null && data.items != null) {
				MyListAdapter adapter = new MyListAdapter(data.items);
				setListAdapter(adapter);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		RYHttpClient
				.getMyDirectMessageList(getActivity(), mHttpResponseHandler);
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ADirectMessage ad = (ADirectMessage) getListAdapter().getItem(
						position);
				if (ad != null) {
					startActivity(new Intent(getActivity(),
							DirectMessageDetailActivity.class).putExtra("name",
							ad.username));
				}

			}
		});
	}

	private class ViewHolder {
		TextView mTitleView;
		TextView mTimeView;
		TextView mContentView;
		NetworkImageView mAvatarView;
	}

	private class MyListAdapter extends ArrayAdapter<ADirectMessage> {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyListAdapter(List<ADirectMessage> list) {
			super(getActivity(), -1, list);
			mQueue = Volley.newRequestQueue(getActivity());
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_notify, parent, false);

				ViewHolder holder = new ViewHolder();
				holder.mTitleView = (TextView) convertView
						.findViewById(R.id.title);
				holder.mTimeView = (TextView) convertView
						.findViewById(R.id.time);
				holder.mContentView = (TextView) convertView
						.findViewById(R.id.content);
				holder.mAvatarView = (NetworkImageView) convertView
						.findViewById(R.id.avatar);

				convertView.setTag(holder);
			}

			ViewHolder holder = (ViewHolder) convertView.getTag();
			ADirectMessage item = getItem(position);

			// TextAppearanceSpan localAppearance = new TextAppearanceSpan(
			// getActivity(), R.style.message_list_title);
			// SpannableString spannableString = new
			// SpannableString(item.username + " 给你发来了私信");
			// spannableString.setSpan(localAppearance, item.username.length(),
			// spannableString.length(),
			// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			// holder.mTitleView.setText(spannableString, BufferType.SPANNABLE);
			holder.mTitleView.setText(item.username);

			holder.mContentView.setText("“" + item.content + "”");
			holder.mTimeView.setText(item.datetime);
			if (!TextUtils.isEmpty(item.avatar_read)) {
				holder.mAvatarView.setImageUrl(item.avatar_read, mImageLoader);
			}

			return convertView;

		}
	}
}
